package com.anos.smsfun.utility;

import android.content.Context;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.style.ImageSpan;
import com.anos.smsfun.R;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by Admin on 4/1/2014.
 */
public class EmoticonsMgr {

    public static final HashMap<String, Integer> mEmoticons = new HashMap();

    static {
        mEmoticons.put(":*", R.drawable.sms_1);
        mEmoticons.put(":|", R.drawable.sms_2);
        mEmoticons.put(";)", R.drawable.sms_3);
        mEmoticons.put(":(", R.drawable.sms_4);

        mEmoticons.put(":q", R.drawable.sms_5);
        mEmoticons.put(":w", R.drawable.sms_6);
        mEmoticons.put(":e", R.drawable.sms_7);
        mEmoticons.put(":r", R.drawable.sms_8);
        mEmoticons.put(":t", R.drawable.sms_9);
        mEmoticons.put(":y", R.drawable.sms_10);
        mEmoticons.put(":u", R.drawable.sms_11);
        mEmoticons.put(":i", R.drawable.sms_12);
        mEmoticons.put(":o", R.drawable.sms_13);
        mEmoticons.put(":p", R.drawable.sms_14);
        mEmoticons.put(":a", R.drawable.sms_15);
        mEmoticons.put(":s", R.drawable.sms_16);
        mEmoticons.put(":d", R.drawable.sms_17);
        mEmoticons.put(":f", R.drawable.sms_18);
        mEmoticons.put(":g", R.drawable.sms_19);
        mEmoticons.put(":h", R.drawable.sms_20);
        mEmoticons.put(":j", R.drawable.sms_21);
        mEmoticons.put(":k", R.drawable.sms_22);
        mEmoticons.put(":l", R.drawable.sms_23);
        mEmoticons.put(":z", R.drawable.sms_24);
        mEmoticons.put(":x", R.drawable.sms_25);
        mEmoticons.put(":c", R.drawable.sms_26);
        mEmoticons.put(":v", R.drawable.sms_27);
        mEmoticons.put(":b", R.drawable.sms_28);
        mEmoticons.put(":n", R.drawable.sms_29);
        mEmoticons.put(":m", R.drawable.sms_30);
        mEmoticons.put(":]", R.drawable.sms_31);
        mEmoticons.put(":1", R.drawable.sms_32);
        mEmoticons.put(":2", R.drawable.sms_33);
        mEmoticons.put(":3", R.drawable.sms_34);
        mEmoticons.put(":4", R.drawable.sms_35);
        mEmoticons.put(":5", R.drawable.sms_36);
        mEmoticons.put(":6", R.drawable.sms_37);
        mEmoticons.put(":7", R.drawable.sms_38);
        mEmoticons.put(":8", R.drawable.sms_39);
        mEmoticons.put(":9", R.drawable.sms_40);
        mEmoticons.put(":0", R.drawable.sms_41);
        mEmoticons.put(":-", R.drawable.sms_42);
        mEmoticons.put(":=", R.drawable.sms_43);
        mEmoticons.put(":[", R.drawable.sms_44);
        mEmoticons.put(":/", R.drawable.sms_45);
    }

    public static Spannable getSmiledText(Context context, String text) {

        SpannableStringBuilder builder = new SpannableStringBuilder(text.toLowerCase());
        int index;

        for (index = 0; index < builder.length(); index++) {
            for (Map.Entry<String, Integer> entry : mEmoticons.entrySet()) {
                int length = entry.getKey().length();
                if (index + length > builder.length())
                    continue;
                if (builder.subSequence(index, index + length).toString().equals(entry.getKey())) {
                    builder.setSpan(new ImageSpan(context, entry.getValue()), index, index + length,
                            Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                    index += length - 1;
                    break;
                }
            }
        }
        return builder;
    }

    /**
     * get key by value
     */
    public static String getKeyByValue(int value){
        for (Map.Entry<String, Integer> entry : mEmoticons.entrySet()) {
            if(entry.getValue() == value){
                return entry.getKey();
            }
        }
        return "";
    }

     /**
     * get list icon from map
     */
    public static ArrayList<Integer> getListIcons(Context context){
        ArrayList<Integer> arr = new ArrayList<Integer>();
        for (Map.Entry<String, Integer> entry : mEmoticons.entrySet()) {
            arr.add(entry.getValue());
        }
        return arr;
    }
}
